<?php

!defined('IN_APP') && exit('Access Denied');

class admin_usergroupcontrol extends controlbase {

  

    /*会员用户组列表*/
    function ondefault($message='') {
        if(empty($message)) unset($message);
        $usergrouplist = $this('usergroup')->get_list(1);
        include template('usergrouplist');
    }

    /*系统用户组列表*/
    function onsystem() {
        $usergrouplist = $this('usergroup')->get_list(0);
        include template('systemgrouplist');
    }

	/*用于更改用户组*/
	function onchange(){
		$uid=intval($this->get['uid']);
		if(isset($this->get['id'])){
			$id=intval($this->get['id']);
			$this('user')->update($uid,array('groupid'=>$id));
			$this->message('更改用户组成功！','c=admin_user');
		}
		$usergrouplist = $this('usergroup')->get_list(0);
		include template('changeusergroup');
	}
	
    /*添加会员组*/
    function onadd() {
        $group['title']=trim($this->post['title']);
        if($group['title']) {
		$group['type']=1;
		$group['regulars']='';
		$this('usergroup')->add($group);
		$this->ondefault('添加会员组成功！');
        }
    }

    /*删除会员组，如果本组有会员存在，则不可删除*/
    function onremove() {
        $id =intval($this->get['id']);
        $this('usergroup')->remove($id);
        $this->ondefault('删除组成功！');
    }

    /*设置权限*/
    function onregular() {
        $id =intval($this->get['id']);
        $group = $this('usergroup')->get($id);
        if(isset($this->post['regular_code'])) {
            $set['regulars']=implode(',',$this->post['regular_code']);
            $this('usergroup')->update($id,$set);
            $message='组权限设置成功！';
        }
        $this->cache->remove('usergroup');
        include template('editusergroup');
    }


    /*编辑组名*/
    function onedit() {
        $ids =$this->post['id'];
        $titles =$this->post['title'];
        $idcount=count($ids);
        for($i=0;$i<$idcount;$i++) {
            $group = $this('usergroup')->get($ids[$i]);
            $group['title']=$titles[$i];
            $this('usergroup')->update($ids[$i],$group);
        }
        $this->ondefault('用户组更新成功！');
    }
}
